<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>表格渲染</title>
  </head>
  <body>
    <table border="1" cellspacing="0">
      <thead>
        <tr>
          <th>学号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
        </tr>
      </thead>
    </table>
    <script>
      var userList = [
        { id: 1, name: 'Jack', age: 18, gender: '男' },
        { id: 2, name: 'Rose', age: 20, gender: '女' },
        { id: 3, name: 'Tom', age: 22, gender: '男' },
        { id: 4, name: 'Jerry', age: 24, gender: '女' },
      ];
      var table = document.querySelector('table');
      var thead = document.querySelector('thead');
      var tbody = document.createElement('tbody');
      thead.after(tbody);
      //遍历数组
      userList.forEach(function (item) {
        console.log(item);
        var tr = document.createElement('tr');
        tbody.append(tr);
        //遍历对象
        for (var key in item) {
          console.log(key);
          var td = document.createElement('td');
          tr.append(td);
          td.textContent = item[key];
        }
      });
    </script>
  </body>
</html>
